﻿/*
The MIT License

Copyright (c) 2009 Gil Beyruth

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

package com.firstpixel.multiuser
{
	import flash.events.EventDispatcher;
	import flash.events.Event;
	import flash.net.*
	import flash.events.*;
	import flash.display.*;
	import flash.text.TextField;
	import flash.utils.*;
	import com.firstpixel.utils.StringUtils;
	
	public class Signin extends MovieClip
	{
		private static var _username:String="";
		private static var _password:String="";
		
		private var pathUsername_txt:Object;
		private var pathPassword_txt:Object;
		private var pathLogin_bt:Object;
		
		public function Signin(){
			pathUsername_txt = this.getChildByName(username_txt.name);
			pathPassword_txt = this.getChildByName(password_txt.name);
			pathPassword_txt.displayAsPassword = true;
			pathLogin_bt = this.getChildByName(login_bt.name);
			pathLogin_bt.addEventListener(MouseEvent.CLICK, loginClickHandler);
			pathLogin_bt.addEventListener(MouseEvent.MOUSE_OVER, loginMouseOverHandler);
			pathLogin_bt.addEventListener(MouseEvent.MOUSE_OUT, loginMouseOutHandler);
		}
		static public  function get username():String
		{
			return _username;
		}
		static public  function get password():String
		{
			return _password;
		}
		private function loginClickHandler(evt:MouseEvent)
		{
			_username = pathUsername_txt.text; 
			_password = pathPassword_txt.text; 
			
			if(StringUtils.trim(_username)!=""){
				if(StringUtils.trim(_password)!=""){
					trace("onReadyToLoginHandler::"+ username);
					dispatchEvent(new Event("onReadyToLoginHandler"));
				}else{
						dispatchEvent(new Event("onPasswordErrorHandler"));
				}
					
			}else{
					dispatchEvent(new Event("onUsernameErrorHandler"));
			}
		}
		private function loginMouseOverHandler(evt:MouseEvent)
		{
			evt.currentTarget.gotoAndStop(2);
		}
		private function loginMouseOutHandler(evt:MouseEvent)
		{
			evt.currentTarget.gotoAndStop(1);
		}
		//Event Dispatcher
		protected static var disp:EventDispatcher;
			
		public static function addEventListener(p_type:String, p_listener:Function, p_useCapture:Boolean=false, p_priority:int=0, p_useWeakReference:Boolean=false):void {
			if (disp == null) { disp = new EventDispatcher(); }
			disp.addEventListener(p_type, p_listener, p_useCapture, p_priority, p_useWeakReference);
		}
		public static function removeEventListener(p_type:String, p_listener:Function, p_useCapture:Boolean=false):void {
			if (disp == null) { return; }
			disp.removeEventListener(p_type, p_listener, p_useCapture);
		}
		public static function dispatchEvent(p_event:Event):void {
			if (disp == null) { return; }
			disp.dispatchEvent(p_event);
		}
		
		// Public API that dispatches an event
		public static function onLoginSuccess():void {
			dispatchEvent(new Event("onLoginSuccess"));
		}
		
		
	}
	
}